package com.naiterui.ehp.bs.doctor.controller.inner;

import cn.hutool.core.collection.CollectionUtil;
import com.naiterui.ehp.bp.bo.doctor.DoctorBO;
import com.naiterui.ehp.bp.bo.doctor.PatientDiseaseBO;
import com.naiterui.ehp.bp.bo.doctor.PcUserBO;
import com.naiterui.ehp.bp.bo.doctor.RecomDoctorBO;
import com.naiterui.ehp.bp.bo.doctor.RecomPatientBO;
import com.naiterui.ehp.bp.bo.doctor.RecomUserInfoBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.IDoctorCertificateVoucherService;
import com.naiterui.ehp.bs.doctor.service.IDoctorService;
import com.naiterui.ehp.bs.doctor.service.IPatientDiseaseService;
import com.naiterui.ehp.bs.doctor.service.IPatientService;
import com.naiterui.ehp.bs.doctor.vo.PatientNameVO;
import com.naiterui.ehp.bs.doctor.vo.certification.DoctorRecordDTO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 用于内部服务提供用户信息数据
 *
 * @author Amber
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("ad/inner/userinfo")
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.INNER_API + SwaggerTag.RELATIONSHIP})
public class AdInnerUserInfoControler {

    private static Logger LOGGER = LoggerFactory.getLogger(AdInnerUserInfoControler.class);

    @Autowired
    private IDoctorService doctorService;
    @Autowired
    private IPatientService patientService;
    @Autowired
    private IDoctorCertificateVoucherService doctorCertificateVoucherService;
    @Autowired
    private IPatientDiseaseService patientDiseaseService;

    /**
     * 为内部服务之间提供用户信息--推荐用药相关
     * InnerUserInfoControoler.getUserInfoForRecom()
     *
     * @param doctorId  医生id
     * @param patientId 患者id
     *
     * @return
     *
     * @throws BusinessException 医患关系不正确、医生账户状态不正常（禁用）、患者账户状态不正常（禁用）
     * @Author Amber
     * @Date 2017年7月31日
     * @since 1.0.0
     */
    @RequestMapping("/recom")
    public ResponseVO<RecomUserInfoBO> getUserInfoForRecom(Long doctorId, Long patientId, Integer fetchNickname) throws BusinessException {

        LOGGER.info("为内部服务之间提供用户信息--推荐用药相关 请求参数：doctorId = [{}], patientId = [{}]", doctorId, patientId);

        // 2017/8/2 王泽浩 为内部服务之间提供用户信息--推荐用药相关
        RecomUserInfoBO userInfoVO = new RecomUserInfoBO();

        if (doctorId != null) {

            RecomDoctorBO innerDoctor = this.doctorService.getRecomDoctorInfo(doctorId);
            userInfoVO.setDoctor(innerDoctor);

        }

        if (patientId != null) {
            RecomPatientBO innerPatient = this.patientService.getInnerPatient(doctorId, patientId, fetchNickname);
            userInfoVO.setPatient(innerPatient);
        }

        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, userInfoVO);

    }


    /**
     * 患者备注名反写--发送处方
     *
     * @param doctorId    医生ID
     * @param patientId   患者ID
     * @param patientName 填写姓名
     *
     * @return
     *
     * @throws BusinessException
     * @Description
     * @author wangchuan
     * @Date 2017年7月31日
     * @since V2.9.0
     */
    @RequestMapping("save/remarkname")
    public ResponseVO<Boolean> saveRemarkName(Long doctorId, Long patientId, String patientName, Integer gender, Integer age, String ageUnit)
            throws BusinessException {
        LOGGER.info("患者备注名反写  请求参数：doctorId = [{}], patientId = [{}], patientName = [{}],gender=[{}],age=[{}],ageUnit=[{}]", doctorId, patientId,
                    patientName, gender, age, ageUnit);
        if (doctorId == null || patientId == null || StringUtils.isBlank(patientName) || gender == null || age == null
                || StringUtils.isBlank(ageUnit)) {
            LOGGER.info("参数错误");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        boolean changedRemarkname = this.patientService.saveRemarkname(doctorId, patientId, patientName, gender, age, ageUnit);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, changedRemarkname);
    }

    /**
     * 查询医生备案、认证相关信息
     *
     * @param doctorId
     *
     * @return
     *
     * @Author Ldl
     * @Date 2017年8月7日
     * @since 1.0.0
     */
    @RequestMapping("doctorRecordInfo")
    public ResponseVO<DoctorRecordDTO> getDoctorRecordInfo(Long doctorId) throws BusinessException {
        DoctorRecordDTO doctorRecord = this.doctorCertificateVoucherService.getDoctorRecordInfo(doctorId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, doctorRecord);
    }

    /**
     * 获取医生对应患者名称信息
     * InnerUserInfoControler.getDoctorPaientName()
     *
     * @param doctorId
     * @param namekey
     *
     * @return
     *
     * @throws BusinessException
     * @Author mudou
     * @Date 2017年8月24日
     * @since 2.9.0
     */
    @RequestMapping("patientNames")
    @ResponseBody
    public ResponseVO<Map<Long, PatientNameVO>> getPaientNames(Long doctorId, String namekey) throws BusinessException {

        if (null == doctorId) {
            LOGGER.error("[获取医生对应患者名称信息] getPaientNames ERROR ; param error : doctorId={},namekey={}", doctorId, namekey);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        namekey = StringUtils.isBlank(namekey) ? null : namekey.trim();

        Map<Long, PatientNameVO> patientNameMap = this.patientService.getPatientNames(doctorId, namekey);

        return PublicService.returnResponseVO(patientNameMap);
    }


    /**
     * 获取医生填写病情
     * InnerUserInfoControler.getDrDisease()
     *
     * @param doctorId
     * @param patientId
     *
     * @return
     *
     * @Author lichaopi
     * @Date 2018年6月2日
     * @since 1.0.0
     */
    @RequestMapping("drDisease")
    @ResponseBody
    public ResponseVO<PatientDiseaseBO> getDrDisease(Long doctorId, Long patientId) throws BusinessException {
        LOGGER.info("获取医生填写病情参数,doctorId:{},patientId:{}", doctorId, patientId);
        boolean verify = PublicService.paramsHasNull(doctorId, patientId);
        if (verify) {
            LOGGER.error("获取医生填写病情,参数错误,doctorId:{},patientId", doctorId, patientId);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        PatientDiseaseBO bo = this.patientDiseaseService.getDrDisease(doctorId, patientId);
        return PublicService.returnResponseVO(bo);
    }

    /**
     * 获取医生列表
     *
     * @param doctorIds
     *
     * @return
     *
     * @throws BusinessException
     */
    @RequestMapping("doctors")
    @ResponseBody
    public ResponseVO<Map<Long, DoctorBO>> getPaientNames(@RequestBody List<Long> doctorIds) throws BusinessException {
        LOGGER.info("获取医生列表：doctorIds={}", doctorIds);
        if (CollectionUtil.isEmpty(doctorIds)) {
            LOGGER.error("[获取医生信息请求参数为空] ");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        Map<Long, DoctorBO> doctors = this.doctorService.getDoctorsByIds(doctorIds);

        return PublicService.returnResponseVO(doctors);
    }

    @GetMapping("doctor")
    @ResponseBody
    public ResponseVO<PcUserBO> getUserInfo(String phone) {
        log.info("根据手机号获取用户信息，phone {}", phone);
        return PublicService.returnResponseVO(this.doctorService.getUserInfo(phone));
    }

    @GetMapping("doctor/face")
    @ResponseBody
    public ResponseVO<PcUserBO> getUserFaceInfo(String phone, String h5SuccessUrl, String h5FailUrl) {
        log.info("根据手机号获取用户人脸识别信息，phone {} {} {}", phone, h5SuccessUrl, h5FailUrl);
        return PublicService.returnResponseVO(this.doctorService.getUserFaceInfo(phone, h5SuccessUrl, h5FailUrl));
    }


    @RequestMapping("/recom/doctor/info")
    public ResponseVO<RecomDoctorBO> getRecomDoctorInfo(Long doctorId) throws BusinessException {
        LOGGER.info("处方详情医生信息 doctorId = {}", doctorId);
        RecomDoctorBO innerDoctor = this.doctorService.getRecomDoctorInfo(doctorId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, innerDoctor);

    }

}
